package com.thoughtworks.biblioteca;

import java.sql.*;

//Understands the authentication of the users into the system.
public class Login {
	Connection connectionToBibliotecaDatabase = null;

	public Login() {
		connectionToBibliotecaDatabase = MySQLConnection.obtainConnection();
	}

	public boolean authenticateUser(String userName, String password) {
		try {
			Statement selectUserDetails = connectionToBibliotecaDatabase
					.createStatement();
			ResultSet userDetailsRecordSet = selectUserDetails
					.executeQuery("SELECT username,password FROM users");
			userDetailsRecordSet.first();
			do {
				if (userName.equals(userDetailsRecordSet.getString("username"))) {
					if (password.equals(userDetailsRecordSet
							.getString("password"))) {
						return true;
					}
				}
			} while (userDetailsRecordSet.next());
		} catch (Exception e) {
		}
		return false;
	}
}